package petrochina.company.electric.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.tencent.mmkv.MMKV;

import petrochina.company.electric.R;
import petrochina.company.electric.app.AppActivity;
import petrochina.company.electric.http.api.LoginApi;
import petrochina.company.electric.http.model.HttpData;
import petrochina.company.electric.model.LoginSubmitData;

/**
 * desc: 登录
 */
public final class LoginActivity extends AppActivity {

    private static final String INTENT_KEY_IN_LOGIN = "login";

    public static void start(Context context) {

        Intent intent = new Intent(context, LoginActivity.class);
//        intent.putExtra(INTENT_KEY_IN_LOGIN, loginResult);
//        if (!(context instanceof Activity)) {
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        }
        context.startActivity(intent);
    }

    private Gson mGson;
    private MMKV mmkv;

    private EditText etName, etPwd;
    private Button btnLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.user_login;
    }

    @Override
    protected void initView() {
        mGson = new Gson();
        mmkv = MMKV.defaultMMKV();

        etName = findViewById(R.id.et_name);
        etPwd = findViewById(R.id.et_pwd);

        btnLogin = findViewById(R.id.btn_login);
        setOnClickListener(btnLogin);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_login) {
            String userName = etName.getText().toString().trim();
            String passWord = etPwd.getText().toString().trim();
            if (TextUtils.isEmpty(userName)) {
                ToastUtils.show("用户名不能为空！");
                return;
            } else if (TextUtils.isEmpty(passWord)) {
                ToastUtils.show("密码不能为空！");
                return;
            }
            Gson gson = new Gson();
            LoginSubmitData loginSubmitData = new LoginSubmitData(userName, passWord);
            String json = gson.toJson(loginSubmitData);
            EasyHttp.post(this)
                    .api(new LoginApi())
                    .json(json)
                    .request(new HttpCallback<HttpData<LoginApi.Bean>>(this) {

                        @Override
                        public void onSucceed(HttpData<LoginApi.Bean> data) {
                            String dataJson = mGson.toJson(data.getData());
                            mmkv.putString("loginData", dataJson);
                            HomeActivity.start(LoginActivity.this);
                            finish();
                        }
                    });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}